﻿namespace Vin.Tool.Core.DateTimeCategory;

/// <summary>
/// 时间戳处理工具类
/// </summary>
public class TimestampTool
{
    /// <summary>
    /// 获取当前时间戳（毫秒级）
    /// </summary>
    /// <returns>当前时间戳（毫秒级）</returns>
    public static long GetCurrentTimestamp()
    {
        return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
        // DateTime dt = DateTime.UtcNow;
        // TimeSpan ts = dt - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        // return (long)ts.TotalMilliseconds;
    }

    /// <summary>
    /// 将时间戳（毫秒级）转换为 DateTime 类型
    /// </summary>
    /// <param name="timestamp">时间戳（毫秒级）</param>
    /// <returns>转换后的 DateTime 类型</returns>
    public static DateTime ConvertToDateTime(long timestamp)
    {
        return DateTimeOffset.FromUnixTimeMilliseconds(timestamp).ToLocalTime().DateTime;
    }

    /// <summary>
    /// 将 DateTime 类型转换为时间戳（毫秒级）
    /// </summary>
    /// <param name="dateTime">DateTime 类型</param>
    /// <returns>转换后的时间戳（毫秒级）</returns>
    public static long ConvertToTimestamp(DateTime dateTime)
    {
        TimeSpan ts = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        return (long)ts.TotalMilliseconds;
    }

    /// <summary>
    /// 获取当前时间戳（秒级）
    /// </summary>
    /// <returns>当前时间戳（秒级）</returns>
    public static long GetCurrentTimestampSeconds()
    {
        return DateTimeOffset.UtcNow.ToUnixTimeSeconds();
        // return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
    }

    /// <summary>
    /// 将时间戳（秒级）转换为 DateTime 类型
    /// </summary>
    /// <param name="timestamp">时间戳（秒级）</param>
    /// <returns>转换后的 DateTime 类型</returns>
    public static DateTime ConvertToDateTimeSeconds(long timestamp)
    {
        return DateTimeOffset.FromUnixTimeSeconds(timestamp).ToLocalTime().DateTime;
    }

    /// <summary>
    /// 将 DateTime 类型转换为时间戳（秒级）
    /// </summary>
    /// <param name="dateTime">DateTime 类型</param>
    /// <returns>转换后的时间戳（秒级）</returns>
    public static long ConvertToTimestampSeconds(DateTime dateTime)
    {
        return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
    }

    /// <summary>
    /// 毫秒转天时分秒
    /// </summary>
    /// <param name="timestamp"></param>
    /// <returns></returns>
    public static string FormatTime(long timestamp)
    {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = timestamp / dd;
        long hour = (timestamp - day * dd) / hh;
        long minute = (timestamp - day * dd - hour * hh) / mi;
        long second = (timestamp - day * dd - hour * hh - minute * mi) / ss;
        // long milliSecond = timestamp - day * dd - hour * hh - minute * mi - second * ss;

        string sDay = day < 10 ? "0" + day : "" + day; //天
        string sHour = hour < 10 ? "0" + hour : "" + hour; //小时
        string sMinute = minute < 10 ? "0" + minute : "" + minute; //分钟
        string sSecond = second < 10 ? "0" + second : "" + second; //秒
        // string sMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond; //毫秒
        // sMilliSecond = milliSecond < 100 ? "0" + sMilliSecond : "" + sMilliSecond;

        return $"{sDay} 天 {sHour} 小时 {sMinute} 分 {sSecond} 秒";
    }
}